/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022.  All rights reserved.2023.
 */

package service

import (
	"errors"
	"prim-server/common"
	"prim-server/dao"
	"prim-server/logger"
	"prim-server/model"
	"prim-server/util"
	"time"
)

// 增加资源更新记录
func CreateAndSaveOrderUpdatetRecord(userId string,
	onePayOrderUpdateTime, onDemandResourceUpdateTime time.Time,
	isUTC bool) error {
	orderUpdateRecord := &model.OrderUpdateRecord{
		UserId:                     userId,
		OnePayOrderUpdateTime:      onePayOrderUpdateTime,
		OnDemandResourceUpdateTime: onDemandResourceUpdateTime,
	}
	if !isUTC {
		orderUpdateRecord.OnePayOrderUpdateTime = util.GetUTC(onePayOrderUpdateTime)
		orderUpdateRecord.OnDemandResourceUpdateTime = util.GetUTC(onDemandResourceUpdateTime)
	}
	err := dao.GetOrderUpdateRecordDao().SaveOrderUpdateRecordInfo(orderUpdateRecord)
	if err != nil {
		logger.Error("crate order update record error: %v, recordId: %s", err.Error(), orderUpdateRecord.IndexId)
		return errors.New("创建资源更新记录失败")
	}
	return nil
}

// 创建用户默认资源更新记录
func CreateDefaultUpdateRecord(userId string) error {
	now := util.GetUTC(time.Now())
	return CreateAndSaveOrderUpdatetRecord(userId, now, now, true)
}

// 删除资源更新记录
func DeleteOrderUpdateRecord(userId string) error {
	err := dao.GetOrderUpdateRecordDao().DeleteOrderUpdateRecordInfo(userId)
	if err != nil {
		logger.Error("delete order update record error: %v, userId: %s", err.Error(), userId)
		return errors.New("删除资源更新记录失败")
	}
	return nil
}

// 查询资源更新记录
func GetOrderUpdateRecordOneWithUserId(userId string) (*model.OrderUpdateRecord, error) {
	orderUpdateRecords, _, err := GetOrderUpdateRecordAll([]string{userId})
	if err != nil {
		logger.Error("get order update record error%v, userId: %s", err.Error(), userId)
		return nil, errors.New("查询资源更新记录失败")
	}
	if len(orderUpdateRecords) == 0 {
		return nil, nil
	}
	return orderUpdateRecords[0], nil
}
func GetOrderUpdateRecordAll(userId []string) ([]*model.OrderUpdateRecord, int, error) {
	queryParameter := &common.QueryOrderUpdateRecordDetail{
		UserId: userId,
	}
	return GetOrderUpdateRecordAllWithParameter(queryParameter)
}
func GetOrderUpdateRecordAllWithParameter(queryParameter *common.QueryOrderUpdateRecordDetail) ([]*model.OrderUpdateRecord, int, error) {
	if queryParameter == nil {
		return nil, 0, nil
	}
	orderUpdateRecords, totalCount, err := dao.GetOrderUpdateRecordDao().GetOrderUpdateRecordInfo(queryParameter)
	if err != nil {
		logger.Error(err.Error())
		return nil, 0, errors.New("查询资源更新记录失败")
	}
	return orderUpdateRecords, totalCount, nil
}

// 更新资源更新记录
func UpdateOrderRecord(userId string, onePayUpdateTime, onDemandUpateTime *int64) error {
	orderUpdateRecord := &common.ModifyOrderUpdateRecordDetail{
		UserId:                     userId,
		OnePayOrderUpdateTime:      onePayUpdateTime,
		OnDemandResourceUpdateTime: onDemandUpateTime,
	}
	err := dao.GetOrderUpdateRecordDao().UpdateOrderUpdateRecordInfo(orderUpdateRecord)
	if err != nil {
		logger.Error("update order record error: %v, userId: %s", err.Error(), userId)
		return errors.New("更新资源记录失败")
	}
	return nil
}
